<?php
/**
 * @version		$Id: virtuemart.php 26 2010-09-08 15:30:31Z joomlaworks $
 * @package		K2mart
 * @author    JoomlaWorks http://www.joomlaworks.gr
 * @copyright	Copyright (c) 2006 - 2010 JoomlaWorks, a business unit of Nuevvo Webware Ltd. All rights reserved.
 * @license		Commercial - This code cannot be redistributed without permission from Nuevvo Webware Ltd.
 */

defined('_JEXEC') or die ('Restricted access');

class JElementVirtuemart extends JElement {

	function fetchElement($name, $value, & $node, $control_name) {
		
		//Load language 
		JPlugin::loadLanguage('plg_k2_k2mart', JPATH_ADMINISTRATOR);
		
		//Check if Virtuemart is installed
		if(!plgK2K2mart::isInstalled()){
			return JText::_('Virtuemart was not found on your system');
		}

		//Import Tabs
		jimport('joomla.html.pane');
		$pane = & JPane::getInstance('Tabs');
		$params = &JComponentHelper::getParams('com_k2mart');
				
		//Add scripts and styles
		$document = &JFactory::getDocument();
		$document->addScript(JURI::root().'plugins/k2/k2mart/includes/js/script.js');
		$document->addScript(JURI::root().'components/com_virtuemart/js/product_attributes.js');
		$document->addStyleSheet(JURI::root().'plugins/k2/k2mart/tmpl/css/style.css');
		$document->addScriptDeclaration('var k2martLiveSite="'.JURI::root().'";');

		//Get K2 Item
		$itemID = JRequest::getInt('cid');
		$productID = NULL;
		if($itemID){
			$db = &JFactory::getDBO();
			$query = "SELECT productID FROM #__k2mart WHERE itemID={$itemID}";
			$db->setQuery($query);
			$productID = $db->loadResult();
		}

		//Include Virtuemart classes
		global $ps_product, $ps_product_category;
		require_once(JPATH_SITE.DS.'components'.DS.'com_virtuemart'.DS.'virtuemart_parser.php');
		require_once(CLASSPATH.'ps_product_discount.php');
		include_class('product');
		include_class('category');
		include_class('manufacturer');
		include_class('tax');
		
		//Get Virtuemart Database
		$db = new ps_DB;
		
		//Get product
		$product = $ps_product->get($productID);
		
		//Get categories
		$categories = array();
		$q = "SELECT category_id FROM #__{vm}_product_category_xref WHERE product_id='$productID'";
		$db->query($q);
		while ($db->next_record()) {
			$categories[$db->f("category_id")] = "1";
		}
		if(empty($categories)){
			$categories[0] = "1";
		}
		
		//Get Manufacturer
		$db->query("SELECT manufacturer_id FROM #__{vm}_product_mf_xref WHERE product_id='$productID'");
		$db->next_record();
		$manufacturerID = $db->f("manufacturer_id");
		
		//Get Price
		if (!empty($productID)) {
			$price = $ps_product->get_retail_price($productID);
		} else {
			$price['product_price'] = vmGet($_REQUEST,'product_price', '');
		}
		$quantity_start = 0;
		$quantity_end = 0;
		$quantity_step = 1;
		if(empty($price["product_currency"])) {
			$price["product_currency"] = $vendor_currency;
		}
	
		//Get Shopper Group
		$q =  "SELECT shopper_group_id,shopper_group_name FROM #__{vm}_shopper_group WHERE `default`= '1' AND vendor_id='".$product->f("vendor_id")."'";
		$db->query($q);
		if ($db->num_rows()<1) {  // when there is no "default", take the first in the table
			$q =  "SELECT shopper_group_id,shopper_group_name from #__{vm}_shopper_group WHERE vendor_id='$ps_vendor_id'";
			$db->query($q);
		}
		$db->next_record();
		$shopperGroupID = $db->f("shopper_group_id");
		$shopperGroupName = $db->f("shopper_group_name");

		//Get min max order levels
		$order_levels = ps_product::product_order_levels($productID);
		if($order_levels) {
			$min_order = array_shift($order_levels);
			$max_order = array_shift($order_levels);
		}
		
		//Get Availability Date
		$availabilityDate = JHTML::_('date', $product->sf("product_available_date"), '%Y-%m-%d');
		
		//Get discount
		$discounts = ps_product_discount::discount_list( $product->sf("product_discount_id") );
		if(stripos($discounts, '<a href=')){
			$discounts = JText::_('None');
		}
		
		//Output
		ob_start();
		include JPATH_SITE.DS.'plugins'.DS.'k2'.DS.'k2mart'.DS.'tmpl'.DS.'form.php';
		$contents = ob_get_clean();
		return $contents;

	}

	function fetchTooltip($label, $description, &$xmlElement, $control_name='', $name='') {
		return NULL;
	}

}
